define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'paotui/member/index' + location.search,
                    add_url: '',
                    edit_url: '',
                    del_url: 'paotui/member/del',
                    multi_url: '',
                    import_url: '',
                    table: 'ttk_third',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'level_name', title: __('会员等级'), operate: false,formatter:Controller.api.formatter.level_name},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('头像'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'num', title: __('发件数'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'content',
                                    title: __('设置等级'),
                                    text:'设置等级',
                                    extend:'data-area=["600px","400px"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-vcard',
                                    url: 'paotui/member/set_level'
                                },
                                ],formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        set_level: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter:{
                level_name: function (value, row, index) {
                    switch (value) {
                        case null:
                            return "普通用户";
                        default:
                            return value;
                    }
                },
                type: function (value, row, index) {
                    switch (value) {
                        case "miniapp":
                            return "小程序";
                    }
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});